package com.szcinda.fitness.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.szcinda.fitness.TraineeService;
import com.szcinda.fitness.controller.utils.Result;
import com.szcinda.fitness.controller.utils.WechatAuthCodeResponse;
import com.szcinda.fitness.controller.utils.WechatAuthProperties;
import com.szcinda.fitness.persistence.Trainee;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@RestController
@RequestMapping("/wechat")
public class WechatController {

    private final RestTemplate restTemplate;

    private final TraineeService traineeService;

    private final WechatAuthProperties wechatAuthProperties;

    public WechatController(TraineeService traineeService, WechatAuthProperties wechatAuthProperties) {
        this.traineeService = traineeService;
        this.wechatAuthProperties = wechatAuthProperties;
        this.restTemplate = new RestTemplate();
    }

    @PostMapping("getOpenId")
    public Result getOpenId(@RequestParam String code) {
        WechatAuthCodeResponse response = getWxSession(code,1);
        return returnResult(response);
    }

    private Result returnResult(WechatAuthCodeResponse response){
        System.out.println(response);
        if (response.getErrCode() == 0) {
            return Result.success(response.getOpenId());
        } else {
            return Result.fail(response.getErrMsg());
        }
    }


    @PostMapping("getOpenIdByCoach")
    public Result getOpenIdByCoach(@RequestParam String code) {
        WechatAuthCodeResponse response = getWxSession(code,2);
        return returnResult(response);
    }

    //这里后面优化可以分为两步,先根据openId判断有没有记录,没有再调另外一个接口传trainee全部信息
    //这里一步做完有点问题,因为OPENID获取比较晚,可能进入此方法时OPENID为空,需加个判断
    @PostMapping("check")
    public Result check(@RequestBody Trainee trainee) {
        //如果没有OPENID,直接返回
        if(trainee.getOpenId()==null || trainee.getOpenId().length()==0){
            return Result.fail("没有OPENID,无法检查信息");
        }
        traineeService.check(trainee);
        return Result.success();
    }

    private WechatAuthCodeResponse getWxSession(String code,int type) {
        String urlString = "?appid={appid}&secret={srcret}&js_code={code}&grant_type={grantType}";
        String response = restTemplate.getForObject(
                wechatAuthProperties.getSessionHost() + urlString, String.class,
                type==1?wechatAuthProperties.getAppId():wechatAuthProperties.getCoachAppId(),
                type==1?wechatAuthProperties.getSecret():wechatAuthProperties.getCoachSecret(),
                code,
                wechatAuthProperties.getGrantType());
        ObjectMapper objectMapper = new ObjectMapper();
        WechatAuthCodeResponse wechatAuthCodeResponse;
        try {
            wechatAuthCodeResponse = objectMapper.readValue(response, WechatAuthCodeResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
            wechatAuthCodeResponse = new WechatAuthCodeResponse();
            wechatAuthCodeResponse.setErrCode(-1);
            wechatAuthCodeResponse.setErrMsg(e.getMessage());
        }
        return wechatAuthCodeResponse;
    }
}
